package com.topscomm.gpm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.gpm.GpmScript;
import com.topscomm.gpm.pojo.GpmProjectEntity;
import com.topscomm.gpm.pojo.GpmProjectProblemEntity;
import com.topscomm.gpm.service.impl.GpmProjectProblemService;
import com.topscomm.gpm.service.impl.GpmProjectService;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description TODO
 * @Classname SdProjectProblemScript
 * @Date 2021/12/17
 * @Created by jiangtao
 */
public class GpmProjectProblemScript extends GpmScript {
	//状态enum 用流程节点名字来同步问题开放状态
	private enum NodeName{
		Start{
			public String toString(){
				return "开始" ;
			}
		}
		,
		Finish{
			public String toString(){
				return "结束" ;
			}
		},
		Open{
			public String toString(){
				return "开放" ;
			}
		},
		Close{
			public String toString(){
				return "关闭" ;
			}
		}

	}

	/***
	 * @description: 同步问题状态和审核状态
	 * @param service
	 * @param entity
	 * @return
	 * @author: jiangtao1
	 * @date: 2021/12/17 14:22
	 * @modify:
	 */
	@Transactional
	public void SyncProblemStatus(ITapService service, TapEntity entity)throws Exception{

		CboApprovalNodeInsEntity nodeinsentity = (CboApprovalNodeInsEntity) entity;
		GpmProjectProblemService problemservice =(GpmProjectProblemService) SpringUtil.getBean("gpmProjectProblemService");
		GpmProjectProblemEntity problementity = problemservice.queryById(ConvertUtil.convertToString(nodeinsentity.getSourceid()));
		//  problem status 与nodeins字段extendfield2 绑定
		//判空 枚举 逻辑注释
		//若当前问题状态为审核中,且尚未走到结束节点
		if(problementity.getState() == 1 && !nodeinsentity.getExtendfield2().contentEquals(NodeName.Finish.toString())) {
			problementity.setProblemstatus(NodeName.Open.toString());
		}else {
			problementity.setProblemstatus(NodeName.Close.toString());
		}
		problemservice.updateEntity(problementity);
	}

	/**
	 * @description:同步项目最新项目经理信息
	 * @author: xinfushuo
	 * @param:项目问题流程的service 和当前nodeins对应的项目问题entity
	 * @throws:
	 * @return:
	 * @date: 2021/8/9
	 * @modify: 8.16 更正之前使用projectcode作为projectid的错误
	 */
	@Transactional
	public void syncProjectInfo(ITapService service, TapEntity entity)throws Exception{
		//获取项目entity和关联的Projec的Entity
		CboApprovalNodeInsEntity nodeInsEntity = (CboApprovalNodeInsEntity) entity;
		GpmProjectProblemService problemService =(GpmProjectProblemService) SpringUtil.getBean("gpmProjectProblemService");
		GpmProjectProblemEntity problemEntity = problemService.queryById(ConvertUtil.convertToString(nodeInsEntity.getSourceid()));
		GpmProjectService projectService =(GpmProjectService)SpringUtil.getBean("gpmProjectService");
		GpmProjectEntity projectEntity  = projectService.queryById(ConvertUtil.convertToString(problemEntity.getProjectid()));
		//获取当前项目经理和工号,不匹配则更新
		boolean managerChanged  = !projectEntity.getProjectmanagercode().contentEquals(problemEntity.getProjectmanagercode()) ;
		boolean levelChanged = !projectEntity.getProjectlevel().contentEquals(problemEntity.getProjectlevel());
		//项目经理信息变更
		if(managerChanged){
			problemEntity.setProjectmanager(projectEntity.getProjectmanager());
			problemEntity.setProjectmanagercode(projectEntity.getProjectmanagercode());
		}
		//项目等级变更
		if(levelChanged){
			problemEntity.setProblemlevel(projectEntity.getProjectlevel());
		}
		//任意一个变更则更新
		if(managerChanged||levelChanged){
			problemService.updateEntity(problemEntity);
		}
	}
}
